<?php defined('RUN_KATANA') or die('Restricted access');
class Provider extends Object
{
	public $Title;
	public $TitleImport;
	public $Contact;
	public $Address;
	public $Zip;
	public $City;
	public $Country;
	public $Tel;
	public $Fax;
	public $Email;
	public $Homepage;
	public $Description;
	public $Active;
	public $Brands;
	public $BrandsLoaded;
	public $Groups;
	public $GroupsLoaded;
	public function ProductProvider() 
	{ 
		self::setDataChanged(false);
		self::setBrandsLoaded(false);
		self::setGroupsLoaded(false);
	}
	
	public function __toString()
	{
		$out  = self::getTitle();
		$out .= ' '.self::getZip();
		$out .= ' '.self::getCity();
		$out .= ' '.self::getCountry();
		$out .= ' '.self::getTel();
		$out .= ' '.self::getEmail();
		return $out;
	}
	
	public function getActive() { return intval($this->Active); }
	public function getLink() 
	{  
		$link  = '?provider='.$this->Id;
		if(intval($_REQUEST['cID']) > 0)
		{ $link .= '&amp;cID='.intval($_REQUEST['cID']); }
		return $link; 
	}
	
	/* Brands */
	public function getBrand($id=0) 
	{
		if($id > 0)
		{
			self::loadBrands();
			if(isset($this->Brands[$id]))
			{ return $this->Brands[$id]; }
		}
	}
	public function getBrands() 
	{
		self::loadBrands();
		return $this->Brands; 
	}
	public function getBrandsLoaded() { return $this->BrandsLoaded; }
	/* Groups */
	public function getGroup($id=0) 
	{
		if($id > 0)
		{
			self::loadGroups();
			if(isset($this->Groups[$id]))
			{ return $this->Groups[$id]; }
		}
	}
	public function getGroups() 
	{
		self::loadGroups();
		return $this->Groups; 
	}
	public function getGroupsLoaded() { return $this->GroupsLoaded; }
	
	public function setActive($val) { $this->Active=intval($val); }
	public function setBrands($val) { $this->Brands=$val; }
	public function setBrandsLoaded($val) { $this->BrandsLoaded=$val; }
	public function setGroups($val) { $this->Groups=$val; }
	public function setGroupsLoaded($val) { $this->GroupsLoaded=$val; }
	public function loadBrands() 
	{
		global $shop;
		global $db;
		$brands = $shop->getBrands();
		
		if((self::getId() > 0) && (self::getBrandsLoaded() == false))
		{
			$sql  = ' SELECT brand_id FROM '.DB_PERFIX.'product_provider_to_brands WHERE provider_id='.self::getId().';';
			$data = $db->fetch($sql, CACHE_TTL);
			if(is_array($data))
			{
				foreach($data as $row)
				{ 
					if($shop->getBrand($row['brand_id']))
					{ $this->Brands[$row['brand_id']] = $shop->getBrand($row['brand_id']); }
				}
			}
			self::setBrandsLoaded(true);
		}
	}
	
	public function addBrand($id=0)
	{
		$id = intval($id);
		if(intval($id) > 0)
		{
			if(!self::getBrand($id))
			{
				global $shop;
				global $db;
				$db->addTablesCombination('provider_id', self::getId(), 'brand_id', $id, 'product_provider_to_brands');
				$this->Brands[$id] = $shop->getBrand($id);
			}
		}
	}
	
	public function remBrand($id=0)
	{
		$id = intval($id);
		if(intval($id) > 0)
		{
			if(self::getBrand($id))
			{
				global $db;
				$db->remTablesCombination('provider_id', self::getId(), 'brand_id', $id, 'product_provider_to_brands');
				unset($this->Brands[$id]);
			}
		}
	}
	
	public function loadGroups()
	{
		global $shop;
		global $db;
		$groups = $shop->getGroups();
		
		if((self::getId() > 0) && (self::getGroupsLoaded() == false))
		{
			$sql  = 'SELECT grp.title, ptg.group_id FROM '.DB_PERFIX.'product_provider_to_groups AS ptg ';
			$sql .= 'LEFT JOIN '.DB_PERFIX.'product_groups AS grp ON grp.id=ptg.group_id WHERE ptg.provider_id='.self::getId().' AND grp.id=ptg.group_id ORDER BY grp.title ASC;';
			$data = $db->fetch($sql, CACHE_TTL);
			if(is_array($data))
			{
				foreach($data as $row)
				{ 
					if($shop->getGroup($row['group_id']))
					{ $this->Groups[$row['group_id']] = $shop->getGroup($row['group_id']); }
				}
			}
			self::setGroupsLoaded(true);
		}
	}
	
	public function addGroup($id=0)
	{
		$id = intval($id);
		if(intval($id) > 0)
		{
			if(!self::getGroup($id))
			{
				global $shop;
				global $db;
				$db->addTablesCombination('provider_id', self::getId(), 'group_id', $id, 'product_provider_to_groups');
				$this->Groups[$id] = $shop->getGroup($id);
			}
		}
	}
	
	public function remGroup($id=0)
	{
		$id = intval($id);
		if(intval($id) > 0)
		{
			if(self::getGroup($id))
			{
				global $db;
				$db->remTablesCombination('provider_id', self::getId(), 'group_id', $id, 'product_provider_to_groups');
				unset($this->Groups[$id]);
			}
		}
	}
	
	public function update()
	{
		if((self::getId() > 0) && (self::getDataChanged()))
		{
			$sql  = 'UPDATE '.DB_PERFIX.'product_providers SET ';
			$sql .= '`title`="'.self::formatText(self::getTitle()).'", ';
			$sql .= '`title_import`="'.self::formatText(self::getTitleImport()).'", ';
			$sql .= '`contact`="'.self::formatText(self::getContact()).'", ';
			$sql .= '`address`="'.self::formatText(self::getAddress()).'", ';
			$sql .= '`zip`="'.self::formatText(self::getZip()).'", ';
			$sql .= '`city`="'.self::formatText(self::getCity()).'", ';
			$sql .= '`country`="'.self::formatText(self::getCountry()).'", ';
			$sql .= '`tel`="'.self::formatText(self::getTel()).'", ';
			$sql .= '`fax`="'.self::formatText(self::getFax()).'", ';
			$sql .= '`email`="'.self::formatText(self::getEmail()).'", ';
			$sql .= '`homepage`="'.self::formatText(self::getHomepage()).'", ';
			$sql .= '`description`="'.self::formatText(self::getDescription()).'" ';
			$sql .= ' WHERE id='.self::getId().' LIMIT 1;';
			mysql_query($sql);
		}
	}
}
?>